#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project ：python常用模块库 
@File ：指定偏移量读写.py
@IDE  ：PyCharm 
@Author ：李涵彬
@Date ：2025/2/20 上午1:14 
"""

from shared_memory import SharedMemory

memory_name = "/shm_test_offset"
memory_size = 1024  # 1KB

# 写入多段数据
with SharedMemory(memory_name, memory_size, create=True) as shm:
	shm.write("First segment", offset=0)
	shm.write("Second segment", offset=100)

# 读取特定偏移量的数据
with SharedMemory(memory_name, memory_size, create=False) as shm:
	first_data = shm.read(offset=0, size=13)  # 读取 "First segment"
	second_data = shm.read(offset=100, size=14)  # 读取 "Second segment"
	print(f"First segment: {first_data}")
	print(f"Second segment: {second_data}")

# 清理
shm = SharedMemory(memory_name, memory_size, create=False)
shm.unlink()
